package com.xukehong.demo.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author: xkh
 * @Date: 2019/3/18 16:00
 * @Version 1.0
 * 通过@Configuration注解，让Spring来加载该类配置
 * 再通过@EnableSwagger2注解来启用Swagger211
 * @ConditionalOnProperty 获取配置开关，线下开true,线上关闭swagger
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger",name = "enable", havingValue = "true")
public class Swagger3 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为加注解@ApiOperation的方法显示接口
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .version("1.0.0")
                .build();
    }

}
